package com.haihui.platform.controller.roles;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.haihui.platform.controller.BaseController;
import com.haihui.platform.entity.Role;
import com.haihui.platform.service.roles.RoleService;
import com.haihui.platform.utils.PrimaryKeyGen;

/*
 * 角色模块
 */
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController{
	
	private static final Logger LOG = Logger.getLogger(RoleController.class);
	
	@Autowired
	private RoleService roleService;
	
	/**
	 * 角色列表
	 */
	@RequestMapping(value="/list")
	public void list(Role role){
		try{
			int count = roleService.count(role);
			List<Map<String, Object>> list = roleService.list(role);
			this.responseWriter(this.getCommonJsonPage(count,list));
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			this.responseWriter(this.getCommonJsonString(false, "查询失败"));
		}
	}
	
	/**
	 * 新增角色
	 * @param role
	 */
	@RequestMapping(value="/add")
	public void add(@RequestBody Role role){
		try{
			role.setRoleId(PrimaryKeyGen.getKey());
			int count = roleService.add(role);
			this.responseWriter(this.getCommonJsonString(true, "新增成功", count));
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			this.responseWriter(this.getCommonJsonString(false, "新增失败"));
		}
	}
	
	/**
	 * 删除角色
	 * @param role
	 */
	@RequestMapping(value="/delete")
	public void delete(Role role){
		try{
			int count = roleService.delete(role);
			this.responseWriter(this.getCommonJsonString(true, "删除成功", count));
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			this.responseWriter(this.getCommonJsonString(false, "删除失败"));
		}
	}
	
	/**
	 * 修改角色
	 * @param role
	 */
	@RequestMapping(value="/update")
	public void update(@RequestBody Role role){
		try{
			int count = roleService.update(role);
				this.responseWriter(this.getCommonJsonString(true, "修改成功", count));
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			this.responseWriter(this.getCommonJsonString(false, "修改失败"));
		}
	}
	
	/**
	 * 角色详情
	 * @param role
	 */
	@RequestMapping(value="/detail")
	public void detail(Role role){
		try{
			Map<String, Object> map = roleService.detail(role);
			this.responseWriter(this.getCommonJsonString(true, "查询成功", map));
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			this.responseWriter(this.getCommonJsonString(false, "查询失败"));
		}
	}
	
	/**
	 * 给角色分配资源
	 * @param roleId
	 * @param resId
	 */
	@Transactional
	@RequestMapping(value="/roleRes")
	public void roleRes(@RequestParam(value="roleId") String roleId, 
			@RequestParam(value="resId") String resId){
		try{
			String[] res = resId.split(",");
			List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
			for(String str : res){
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("roleId", roleId);
				map.put("resId", str);
				list.add(map);
			}
			roleService.delResRole(roleId);
			int count = roleService.batchInsert(list);
			this.responseWriter(this.getCommonJsonString(true, "分配成功", count));
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			this.responseWriter(this.getCommonJsonString(false, "分配失败"));
		}
	}
	
}